package org.ohos.rajawali3d.examples.examples.general;

import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.curves.ArchimedeanSpiral3D;
import org.rajawali3d.curves.ICurve3D;
import org.rajawali3d.curves.LogarithmicSpiral3D;
import org.rajawali3d.materials.Material;
import org.rajawali3d.math.vector.Vector3;
import org.rajawali3d.primitives.Line3D;

import java.util.Stack;

public class SpiralsFragment extends AExampleFragment {

    @Override
    public Component onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        DirectionalLayout ll = new DirectionalLayout(getContext());
        ComponentContainer.LayoutConfig lc = ll.getLayoutConfig();
        lc.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        lc.height = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        ll.setOrientation(DirectionalLayout.VERTICAL);
        ll.setAlignment(LayoutAlignment.TOP);

        Text label = new Text(getContext());
        ComponentContainer.LayoutConfig layoutConfig = label.getLayoutConfig();
        layoutConfig.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        layoutConfig.height = DirectionalLayout.LayoutConfig.MATCH_CONTENT;
        label.setText(ResourceTable.String_spirals_fragment_text_view_spiral_types);
        label.setTextSize(42);
        label.setMultipleLine(true);
        label.setTextAlignment(TextAlignment.CENTER);
        label.setTextColor(new Color(0xFFFFFFFF));
        //label.setHeight(100);
        ll.addComponent(label);

        mLayout.addComponent(ll);

        return mLayout;
    }

    @Override
    public AExampleRenderer createRenderer() {
        return new SpiralsRenderer(getContext(), this);
    }

    private final class SpiralsRenderer extends AExampleRenderer {
        private final int NUM_POINTS = 200;

        SpiralsRenderer(Context context, AExampleFragment fragment) {
            super(context, fragment);
        }

        @Override
        public void initScene() {
            getCurrentCamera().setZ(7);

            //
            // -- Logarithmic Spiral
            //
            final Vector3 start = new Vector3(1.0, 0.0, 0);
            LogarithmicSpiral3D logSpiral = new LogarithmicSpiral3D(0.0625, start, new Vector3(0, 1, 1), true);
            drawSpiralCurve(logSpiral, 0x0000ff, new Vector3(0, 1.5, 0));

            //
            // -- Archimedean Spiral
            //
            ArchimedeanSpiral3D aSpiral = new ArchimedeanSpiral3D(.03125, 1, start, new Vector3(0, 1, -1), false);
            drawSpiralCurve(aSpiral, 0x00ff00, new Vector3(0, -1.5, 0));

        }

        private void drawSpiralCurve(ICurve3D curve, int color, Vector3 position) {
            Material lineMaterial = new Material();

            final double angleStep = (20.0 * Math.PI) / NUM_POINTS;
            Stack<Vector3> points = new Stack<>();
            for (int i = 0; i <= NUM_POINTS; i++) {
                Vector3 point = new Vector3();
                curve.calculatePoint(point, i * angleStep);
                points.add(point);
            }

            Line3D line = new Line3D(points, 1, color);
            line.setMaterial(lineMaterial);
            line.setPosition(position);
            getCurrentScene().addChild(line);
        }
    }

}
